﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Microsoft.Practices.Unity;
using ePlanet.SingleWell.Models;
using ePlanet.SingleWell.Services;
using Web.SystemConfig.SysCache;
using Web.SystemConfig.WorkFlow;
using DreamBuilder.Core.Container;
using ePlanet.SingleWell.Business;
using Web.SystemConfig.Models;
using Web.SystemConfig.Services;
using Web.SystemConfig.Business;
using Web.SystemLogin.Models;
using System.Data;

using Wuqi.Webdiyer;

using DreamBuilder.Common;

namespace Web.Views.SingleWellManage
{
    public partial class SingleWellSearch : PageBase
    {
        private SingleWellModel model = null;
        private int recordCount = 0;
        [Dependency]
        public ISingleWellService m_ISingleWellService
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.xSinglewell.AfterFiltered += new EventHandler(xSinglewell_AfterFiltered);
            this.xSinglewell.ClearFiltered += new EventHandler(xSinglewell_ClearFiltered);
            aspNetPagerSearch.PageSizeChanging += new EventHandler(pagerTransfered_PageSizeChanging);
            if (!IsPostBack)
            {
                BindSingleWellData();
            }
        }

        void xSinglewell_ClearFiltered(object sender, EventArgs e)
        {
            List<string> colNames = new List<string>();
            if (this.DataSourseScheme != null)
            {
                foreach (DataColumn col in this.DataSourseScheme.Columns)
                {
                    colNames.Add(col.ColumnName);
                }
            }
            this.Where = this.xSinglewell.CreateFilterSql(colNames);
            this.aspNetPagerSearch.CurrentPageIndex = 1;
            model = null;
            BindSingleWellData();
        }

        void xSinglewell_AfterFiltered(object sender, EventArgs e)
        {
            List<string> colNames = new List<string>();
            if (this.DataSourseScheme != null)
            {
                foreach (DataColumn col in this.DataSourseScheme.Columns)
                {
                    colNames.Add(col.ColumnName);
                }
            }
            this.Where = this.xSinglewell.CreateFilterSql(colNames);
            this.aspNetPagerSearch.CurrentPageIndex = 1;
            //this.aspNetPagerSearch.PageSize = 10;
            BindSingleWellData();
        }

        private void BindSingleWellData()
        {
            try
            {
                int beginNum = this.aspNetPagerSearch.PageSize * (this.aspNetPagerSearch.CurrentPageIndex - 1);
                int pageSize = this.aspNetPagerSearch.PageSize;
                model = this.m_ISingleWellService.GetTotal(beginNum, pageSize, out recordCount, this.Where, string.Empty);
                if (model != null)
                {
                    DataTable oDataTable = model.SINGLEWELL;
                    if (oDataTable != null)
                    {
                        if (this.DataSourseScheme == null)
                        {
                            this.DataSourseScheme = oDataTable.Clone();
                        }
                        this.gv_SingleWellList.PageSize = pageSize;
                        this.gv_SingleWellList.DataSource = oDataTable;
                        this.gv_SingleWellList.DataBind();
                        this.aspNetPagerSearch.RecordCount = recordCount;
                        this.aspNetPagerSearch.DataBind();
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.Message);
            }
        }

        protected void pagerTransfered_PageSizeChanging(object sender, EventArgs e)
        {
            aspNetPagerSearch.CurrentPageIndex = 1;
            BindSingleWellData();
        }

        protected void aspNetPagerSearch_PageChanging(object sender, PageChangingEventArgs e)
        {
            aspNetPagerSearch.CurrentPageIndex = e.NewPageIndex;
            BindSingleWellData();
        }

        //protected void aspNetPagerSearch_PageSizeChanging(object sender, EventArgs e)
        //{

        //}

        protected void Deleted_Click(Object sender, EventArgs e)
        {
            try
            {
                LinkButton linkButton = sender as LinkButton;
                if (linkButton != null)
                {
                    string archiveId = linkButton.CommandArgument;
                    if (!string.IsNullOrEmpty(archiveId))
                    {
                        try
                        {
                            m_ISingleWellService.DeleteSingleWell(archiveId);
                        }
                        catch (Exception ex)
                        {
                            this.Alert("删除失败：" + ex.Message);
                            return;
                        }
                        DataTable oDataTable1 = this.gv_SingleWellList.DataSource as DataTable;
                        if (oDataTable1 != null)
                        {
                            DataTable oDataTable2 = oDataTable1.Copy();
                            if (oDataTable2.Columns.Contains("DataId"))
                            {
                                foreach (DataRow row in oDataTable2.Rows)
                                {
                                    if (row["DataId"].ToString() == archiveId)
                                    {
                                        oDataTable2.Rows.Remove(row);
                                        this.gv_SingleWellList.DataSource = oDataTable2;
                                        this.gv_SingleWellList.DataBind();
                                        break;
                                    }
                                }
                            }
                        }
                        this.Alert("删除成功！");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string Where
        {
            get
            {
                if (this.ViewState["FilterWhere"] != null)
                {
                    return this.ViewState["FilterWhere"] as string;
                }
                else
                {
                    return " 1=1 "; ;
                }
            }
            set
            {
                this.ViewState["FilterWhere"] = value;
            }
        }

        public DataTable DataSourseScheme
        {
            get
            {
                return this.ViewState["DataSourseScheme"] as DataTable;
            }
            set
            {
                this.ViewState["DataSourseScheme"] = value;
            }
        }
    }
}